package com.op.teachingliteapp.fragment.notif;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.op.teachingliteapp.R;
import com.op.teachingliteapp.bean.CourseNotifBean;
import com.op.teachingliteapp.common.Common;
import com.op.teachingliteapp.common.api.teachinglite.NotifUrl;
import com.op.teachingliteapp.database.dao.CourseNotifDao;
import com.op.teachingliteapp.util.asynctask.HttpPostTask;
import com.op.teachingliteapp.util.asynctask.OnTaskProcessListener;

/**
 * 
 * @author handsome
 *
 */
public class NotifListAdapter extends BaseAdapter {

	private ArrayList<NotifCellDataProvider> list;
	private Context mContext;
	private ImageView icon;
	private DisplayImageOptions options;

	public NotifListAdapter(Context context,
			ArrayList<NotifCellDataProvider> list_message) {

		this.mContext = context;

		// 1. 获得list bean
		// DBHelper dbHelper = new DBHelper(getContext());
		// CourseNotifDao dao = new CourseNotifDao(dbHelper);
		// list = dao.getCoursesByUserId("123");

		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		list = list_message;

	}

	public Context getContext() {
		return mContext;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public NotifCellDataProvider getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, final View convertView,
			final ViewGroup parent) {
		LinearLayout ll = null;
		if (convertView != null) {
			ll = (LinearLayout) convertView;
		} else {
			ll = (LinearLayout) LayoutInflater.from(getContext()).inflate(
					R.layout.item_notif, null);
		}
		final NotifCellDataProvider data = getItem(position);

		icon = (ImageView) ll.findViewById(R.id.notif_view);
		TextView name = (TextView) ll.findViewById(R.id.name_tv);
		TextView title = (TextView) ll.findViewById(R.id.title_tv);
		TextView dec = (TextView) ll.findViewById(R.id.dec_tv);
		TextView time = (TextView) ll.findViewById(R.id.time_tv);
		// 头像设置
		ImageLoader.getInstance().displayImage(data.getImage(), icon, options);

		name.setText(data.getName());
		title.setText(data.getTitle());
		dec.setText(data.getDec());
		time.setText(data.getTime());
		/*
		 * 删除帖子
		 */
		Button deleteNotic = (Button) ll.findViewById(R.id.notic_delete);
		deleteNotic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
				builder.setTitle("确定删除吗？");
				builder.setNeutralButton("删除",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

								List<NameValuePair> params = new ArrayList<NameValuePair>();
								params.add(new BasicNameValuePair(
										Common.Notif.DELETE_COURSE_NOTIF_ID,
										data.getCourseNotifId()));

								HttpPostTask task = new HttpPostTask(mContext,
										NotifUrl.DELETE_COURSE_NOTIF, params);
								task.setTaskListener(new OnTaskProcessListener() {

									@Override
									public void prepare() {
									}

									@Override
									public void completed(String result) {
										getDataFromDatabase(position);
										notifyDataSetChanged();
									}
								});
								task.execute();

							}
						});
				builder.setNegativeButton("取消", null);
				builder.create();
				builder.show();
			}
		});

		return ll;
	}

	/**
	 * 从数据库删除数据
	 */
	public void getDataFromDatabase(int position) {
		SharedPreferences preferences = getContext().getSharedPreferences(
				"TeachingLite", Context.MODE_PRIVATE);
		String getCourseUserId = preferences
				.getString(Common.Prefs.USER_ID, "");

		CourseNotifDao courseNotifDao = new CourseNotifDao(getContext(),
				getCourseUserId);
		CourseNotifBean bean = new CourseNotifBean();
		bean.setId(list.get(position).getCourseNotifId());
		courseNotifDao.delete(bean);

		List<CourseNotifBean> courseNotif = courseNotifDao
				.getNotifByCourseId("1001");
		list.clear();
		// 获取通知内容
		for (int i = 0; i < courseNotif.size(); i++) {
			String title = courseNotif.get(i).getTitle();
			String time = courseNotif.get(i).getCreateTime();
			String context = courseNotif.get(i).getContent();
			String name = courseNotif.get(i).getUserBean().getName();
			String image = courseNotif.get(i).getUserBean().getImageUrl();
			String courseNotifId = courseNotif.get(i).getId();
			String type = courseNotif.get(i).getUserBean().getType();
			list.add(new NotifCellDataProvider(image, name, type,title, context,
					time, courseNotifId));

		}

	}

}
